package com.credit.management.system.web.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.credit.management.system.model.Page;
import com.credit.management.system.model.Project;
import com.credit.management.system.service.ICompanyService;
import com.credit.management.system.service.IProjectService;
import com.credit.management.system.service.ISyroleService;

@Controller
@RequestMapping(value="/project")
public class ProjectController {
	@Autowired
	IProjectService IProjectService;
	@Autowired
	private ICompanyService companyService;
	/**
	 * 进入页面
	 * @return
	 */
	@RequestMapping(value="/project",method=RequestMethod.GET)
	public String list(){
	
		return "project/project";	
	}
	
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findlist(Integer start, Integer length, Object total)
			throws UnsupportedEncodingException {
		if (start == null) {
			start = 0;
		};
		if (length == null) {
			length = 5;
		};
		// 查总条数（单表查询），先去联系怎删改
		Page page=new Page(start,length);
		page=IProjectService.findPageProject(page);
		
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aaData", page.getList());//返回的数据
		map.put("iTotalRecords", page.getTotal());//总条数
		map.put("iTotalDisplayRecords",page.getTotal());////总条数
		return map;
	}
	@RequestMapping(value="/addprojectPre")
	@ResponseBody
	public List<Map<String, Object>> addprojectPre() {
		List<Map<String, Object>> maps=companyService.findAllCompanys();
		
		return maps;
	}
	/**
	 * 添加项目
	 * @param project
	 * @return
	 */
	@RequestMapping(value="/addproject")
	@ResponseBody
	public Map<String, Object> addproject(Project project) {
		System.out.println("********************************" + project);
		Map<String, Object> map = new HashMap<>();
		int num = IProjectService.insert(project);
		map.put("num", num);
		return map;
	}
	/**
	 * 根据id查询项目
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findProjectByid")
	@ResponseBody
	public Project findProjectByid(int id) {
		System.out.println(id+"fgjhklghjkljhjhlk");
		Project project = IProjectService.selectByPrimaryKey(id);
		Map<String, Object> map = new HashMap<>();
		return project;
	}
	/**
	 * 修改项目
	 * @param project
	 * @return
	 */
	@RequestMapping(value = "/updataproject")
	@ResponseBody
	public Map<String, Object> updataupdataproject(Project project) {
		System.out.println(project+"fghjlkjhghjlkjhj");
		Map<String, Object> map = new HashMap<>();
		int num = IProjectService.updateByPrimaryKeySelective(project);
		map.put("num", num);
		return map;
	}
	
	@RequestMapping(value = "/deleteproject1")
	@ResponseBody
	public Map<String, Object> deleteproject(int id){
		//System.out.println("0000000id:"+id);
		IProjectService.deleteByPrimaryKey(id);
		Map<String, Object> map = new HashMap<>();
		return map;
	}
	@RequestMapping(value = "/projectdeleteall")
	@ResponseBody
	public Map<String, Object> deleteallproject(String ids) {
		System.out.println(ids);		
		String[] shuzu = ids.split(",");	
		for (int i = 0; i < shuzu.length; i++) {
			int id=Integer.parseInt(shuzu[i]);
			IProjectService.deleteByPrimaryKey(id);
			System.out.println("id----" + shuzu[i]);
		}
		Map<String, Object> map = new HashMap<>();
		return map;
	}
}
